package org.surelink.radius.model;


import org.ho.yaml.Yaml;
import org.surelink.radius.cache.UserCache;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class User implements IModel,Serializable {
    private String userName;

    private String password;

    private String groupName;

    private int status;

    private Map<String, String> userMetas;

    private static final long serialVersionUID = 1L;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Map<String,String> getUserMetas()
    {
        return userMetas;
    }

    public void setUserMetas(Map<String, String> userMetas)
    {
        this.userMetas = userMetas;
    }

    public String getUserMeta(String key)
    {
        return this.userMetas.get(key);
    }

    public void setUserMeta(String key,String value)
    {
        if(this.userMetas==null)
        {
            this.userMetas = new HashMap<String, String>();
        }
        this.userMetas.put(key,value);
    }

    @Override
    public String toString()
    {
        final StringBuffer sb = new StringBuffer("\r\nUser{");
        sb.append("userName='").append(userName).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", groupName='").append(groupName).append('\'');
        sb.append(", status=").append(status);
        sb.append(", userMetas=").append(userMetas);
        sb.append("}");
        return sb.toString();
    }

    public static void main(String args[])
    {
        File dumpFile = new File("users.yaml");

        try {
            UserCache c = new UserCache();
            User user = new User();
            user.setUserName("wjt");
            user.setPassword("pass");
            user.setGroupName("default");
            user.setStatus(0);
            user.setUserMeta("macAddr", "11:11:11:11");
            c.set(user);

            User user2 = new User();
            user2.setUserName("cyy");
            user2.setPassword("pass");
            user2.setGroupName("default");
            user2.setStatus(0);
            user2.setUserMeta("macAddr","11:11:11:11");
            c.set(user2);

//            Yaml.dump(c.cache, dumpFile);

            Map<String,User> cc = Yaml.loadType(dumpFile, HashMap.class);
            System.out.println(cc.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}